﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace hCommand.task.ip {
    public  class HaIpTask {

        [DllImport("iphlpapi.dll", SetLastError = true)]
        private static extern UInt32 AddIPAddress(UInt32 address, UInt32 ipMask, int ifIndex, out IntPtr nteContext,
                                                  out IntPtr nteInstance);

        public static UInt32 AddIpAddressToInterface(string ipAddress, string subnetMask, int ifIndex) {
            var ipAdd = System.Net.IPAddress.Parse(ipAddress);
            var subNet = System.Net.IPAddress.Parse(subnetMask);
            unsafe {
                //var nteContext = 0;
                var nteInstance = 0;
                IntPtr ptrNteContext;
                var ptrNteInstance = new IntPtr(nteInstance);
                uint ip = (uint)BitConverter.ToInt32(ipAdd.GetAddressBytes(), 0);
                uint mark = (uint)BitConverter.ToInt32(subNet.GetAddressBytes(), 0);
                return AddIPAddress(ip, mark, ifIndex, out ptrNteContext,  out ptrNteInstance);
            }
        }
    }
}
